<template>
	<view class="content">
		<button class="size" @tap="setItem">存储键值</button>
		<button class="size" @tap="setItemAsync">异步存储键值</button>
		<button class="size" @tap="getAllKeys">获取所有键名</button>
		<button class="size" @tap="getAllKeysAsync">异步获取所有键名</button>
		<view v-for="(site,index) in list" :key="index">{{site}}</view>
		<button class="size" @tap="getLength">获取存储数据的个数{{count}}</button>
		<button class="size" @tap="getItem">获取存储的键值{{value}}</button>
		<button class="size" @tap="getItemAsync">异步获取存储的键值{{value}}</button>
		<button class="size" @tap="key">获取键值对中指定索引值的key值</button>
		<button class="size" @tap="removeItem">删除键值</button>
		<button class="size" @tap="removeItemAsync">异步删除键值</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				count: 0,
				value: ''
			}
		},
		methods: {
			setItem() {
				console.log(66)
				// 没有容量限制，但是会影响效率
				plus.storage.setItem("test", "test with hbuilder!");
			},
			setItemAsync() {
				plus.storage.setItemAsync("test8", "test with 2", function() {
					console.log("setItemAsync success");
				}, function(e) {
					console.log("setItemAsync failed: " + JSON.stringify(e));
				});
			},
			getAllKeys() {
				this.list = plus.storage.getAllKeys();
			},
			getAllKeysAsync() {
				let that = this;
				plus.storage.getAllKeysAsync(function(e) {
					console.log(e, "e==")
					that.list = e.keys;
				}, function(e) {
					console.log("clearAsync failed: " + JSON.stringify(e));
				})
			},
			getLength() {
				this.count = plus.storage.getLength();
			},
			getItem() {
				this.value = plus.storage.getItem("test");
			},
			getItemAsync() {
				let that = this;
				plus.storage.getItemAsync("test", function(e) {
					var data = e.data;
					that.value = data;
					console.log("getItemAsync success: " + data);
				}, function(e) {
					console.log("getItemAsync failed: " + JSON.stringify(e));
				});
			},
			key() {
				var keyNames = [];
				var values = [];
				var numKeys = plus.storage.getLength();
				for (var i = 0; i < numKeys; i++) {
					keyNames[i] = plus.storage.key(i);
					values[i] = plus.storage.getItem(keyNames[i]);
				}
				console.log(values, "values==")
			},
			removeItem() {
				plus.storage.removeItem("test");
			},
			removeItemAsync() {
				plus.storage.removeItemAsync("test", function() {
					console.log("removeItemAsync success");
				}, function(e) {
					console.log("removeItemAsync failed: " + JSON.stringify(e));
				});
			}

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
